﻿@using System.Configuration
@using System.Data.SqlClient
@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Unlock Export Page";

    bool hasError = false;
    string errorMessage = string.Empty;
    bool hasSuccess = false;
    string successMessage = string.Empty;

    KeppelLand.Entities.ReportingPeriod lockedReportingPeriod = null;

    if (IsPost)
    {
        if (Request.Form["actionButton"] == "unlock")
        {
            if (KeppelLand.Controllers.ReportingPeriodController.UnlockReportingPeriod())
            {
                hasSuccess = true;
                successMessage = "Reporting period for export unlocked successfully.";
            }

        } // if (Request.Form["actionButton"] == "unlock")

    } // if (IsPost)

    lockedReportingPeriod = KeppelLand.Controllers.ReportingPeriodController.GetLockedReportingPeriod();

}

@if (!KeppelLand.Helpers.UserIdentityHelper.IsTrainer())
{
    @RenderPage("~/Shared/Access_Control.cshtml");
                                                 return;
}

<div class="container-fluid main-content" role="main">

    @RenderPage("~/Shared/Help_Tab.cshtml")

    @if (hasError)
    {
        <div class="alert alert-danger alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @Html.Raw(errorMessage)
        </div>
    }
    @if (hasSuccess)
    {
        <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @successMessage
        </div>
    }

    <h2>
        Unlock Export
    </h2>

    <form method="post" id="unlock_form">

        @if (lockedReportingPeriod == null)
        {
            <p>
                There are no reporting period to unlock.
            </p>
        }
        else
        {
            <p>
                The current exporting period cuts off on @lockedReportingPeriod.EndDate.ToString("dd-MMM-yyyy")
            </p>
            <p>
                Click Unlock Export below to advance reporting period.
            </p>
            <p>
                <button type="button" value="unlock" id="actionButton_unlocked" class="btn btn-primary"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> Unlock Export</button>
                <input type="hidden" name="actionButton" id="hidden_actionButton" />
            </p>
        }
    </form>
</div>

@RenderPage("~/Shared/Standard_Modals.cshtml")

<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="helpModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="helpModalLabel">Help <small>(Unlocked Export)</small></h4>
            </div>
            <div class="modal-body">
                <p>
                    Unlock Export.<br />
                    To export claims approved within this cutoff period claims, follow the below instructions.
                </p>

                <div class="panel panel-default">
                    <div class="panel-heading">Instructions (How should this page work?)</div>
                    <div class="panel-body">
                        <p>
                            This page should be accessible only to HR personel (Lynette).<br />
                            The idea here is that after HR have exported claim records for the reporting period, subsequent exports should export records for the same reporting period.<br />
                            Reporting period will only increment after HR have unlocked export.
                        </p>

                        <ul>
                            <li>HR to click unlock export</li>
                            <li>A modal dialog should display to confirm whether to continue action.</li>

                            <li>If are no export, page should indicate as such or otherwise diable button.</li>
                        </ul>
                    </div>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="helpModal_Close" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

@section Scripts
{
    <script type="text/javascript" src="~/Scripts/kendo/2014.3.1119/kendo.ui.core.min.js"></script>
    <script type="text/javascript" src="~/Scripts/moment.js"></script>
    <script type="text/javascript" src="~/Scripts/moment-with-locales.js"></script>
    <script type="text/javascript">
        (function ($) {
            "use strict";

            $(document).ready(function () {
                // User clicked approve
                $("#actionButton_unlocked").click(function () {
                    $("#hidden_actionButton").val(this.value);
                    $("#confirmationModal #confirmationModalLabel").text("Unlock Export?");
                    $('#confirmationModal').modal('show');
                });
                $("#confirmationModal_OK").click(function () {
                    $("#unlock_form").submit();
                });

            });

        }(jQuery));

    </script>
}